package cn.fanqinghong.system.controller;

import cn.fanqinghong.basic.util.AjaxResult;
import cn.fanqinghong.basic.query.PageList;
import cn.fanqinghong.system.domain.SystemDictionaryDetail;
import cn.fanqinghong.system.query.SystemDictionaryDetailQuery;
import cn.fanqinghong.system.service.ISystemDictionaryDetailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * FAN
 * 2022/6/8  16:24
 */
@RestController
@RequestMapping("/detail")
public class SystemDictionaryDetailController {
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    /**
     *查询全部
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "查询全部")
    public List<SystemDictionaryDetail> loadAll(){
        return systemDictionaryDetailService.queryAll();
    }

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据")
    public SystemDictionaryDetail loadById(@PathVariable("id") Long id){
        return systemDictionaryDetailService.queryById(id);
    }

    /**
     * 查询一条数据
     * @param typeId
     * @return
     */
    @PostMapping("/{typeId}")
    @ApiOperation(value = "查询一条数据")
    public List<SystemDictionaryDetail> loadByTypeId(@PathVariable("typeId") Long typeId){
        return systemDictionaryDetailService.queryByTypeId(typeId);
    }


    /**
     * 删除一条数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            systemDictionaryDetailService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                             .setMessage("写的代码瓦了，我们正在殴打艾正伟程序猿")
                             .setSuccess(false);
        }
    }

    /**
     * 新增或者修改
     * @param systemDictionaryType
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增或者修改")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryType){
        try {
            if(systemDictionaryType.getId() == null){
                systemDictionaryDetailService.save(systemDictionaryType);
                return AjaxResult.me();
            }
            systemDictionaryDetailService.update(systemDictionaryType);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统瓦了，我们正在殴打程序员！！");
        }
    }
    /**
     * 批量删除数据
     * 前端传递的是数组，后端可以使用list或者数组接收
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult batch(@RequestBody List<Long> ids){
        try {
            systemDictionaryDetailService.batch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("抱歉出错了，我们正在联系李荣浩殴打艾正伟程序员！！！");
        }
    }

    /**
     * 分页查询+高级查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "高级查询何分页查询")
    public PageList<SystemDictionaryDetail> queryAll(@RequestBody SystemDictionaryDetailQuery query){
        return systemDictionaryDetailService.queryPage(query);
    }
}
